from rest_framework.response import Response
from rest_framework.views import APIView
from rest_framework.generics import GenericAPIView,ListAPIView
from rest_framework_jwt.authentication import jwt_decode_handler

from six_it.serializerss.gatherings import GatheringsModelserializer
from six_it.serializerss.recruits import Recruitsserializer, Enterpriseserializer, SBiseserializer
from xyz.models import Gathering, Enterprise, Recruit
from rest_framework.mixins import RetrieveModelMixin,UpdateModelMixin,CreateModelMixin

class EnterpriseCreateView(APIView):
    def put(self,request,id):
        ent = Enterprise.objects.get(id=id)
        ent.visits += 1
        ent.save()
        return Response({
            'messgae':'更新成功',
            'success':True
        })
# 职位详情
class RecruitsGetView(RetrieveModelMixin,GenericAPIView):
    queryset = Recruit.objects.all()
    serializer_class = Recruitsserializer
    lookup_field = 'id'

    def get(self, request, id):
        return self.retrieve(request)
# 收藏公司
class PostEnterpriseView(APIView):

    def post(self,request,id):

        token = self.request.auth
        toke_user = jwt_decode_handler(token)
        user_id = toke_user["user_id"]
        cil = Enterprise.objects.get(id=id)
        cil.users.add(user_id)

        return Response({
            'message':'收藏成功',
            'success':True
        })
class SBquxiao(APIView):
    def post(self, request, id):
        token = self.request.auth
        toke_user = jwt_decode_handler(token)
        user_id = toke_user["user_id"]
        cil = Enterprise.objects.get(id=id)
        cil.users.remove(user_id)

        return Response({
            'message': '取消收藏',
            'success': True
        })
class PostRecruitsView(APIView):
    def post(self,request,id):
        token = self.request.auth
        toke_user = jwt_decode_handler(token)
        user_id = toke_user["user_id"]
        cil = Recruit.objects.get(id=id)
        cil.users.add(user_id)
        return Response({
            'message': '收藏成功',
            'success': True
        })
class RecruitsCreateView(APIView):
    def put(self,request,id):
        ent = Recruit.objects.get(id=id)
        ent.visits += 1
        ent.save()
        return Response({
            'messgae':'更新成功',
            'success':True
        })

class EnterpriseGetView(APIView):
    def get(self,request,id):

        ent = Enterprise.objects.get(id=id)
        serializerss = Enterpriseserializer(ent)

        return Response(
        serializerss.data
        )
